package com.itheima.day30.demo01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {
    @org.junit.Test
    public void add() {

        Connection con = null;
        PreparedStatement preparedStatement = null;
        try {
            con = c3p0Utils.getConnection();
            String sql = "insert  into student values (null,?,?,?);";
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, "jack");
            preparedStatement.setInt(2, 22);
            preparedStatement.setString(3, "US");
            final int i = preparedStatement.executeUpdate();
            if (i > 0) {
                System.out.println("add successful!");
            } else {
                System.out.println("add fail!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            c3p0Utils.release(con, preparedStatement);
        }
    }


    @org.junit.Test
    public void Query() {

        Connection con = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            con = c3p0Utils.getConnection();
            String sql = "select *from student;";
            preparedStatement = con.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getInt("sid") + resultSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            c3p0Utils.release(con, preparedStatement, resultSet);
        }
    }
}
